package com.ps.actions.dept;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;

import com.mchange.v2.ser.SerializableUtils;
import com.opensymphony.xwork2.util.ArrayUtils;
import com.ps.actions.BaseAction;
import com.ps.actions.IFBaseAction;
import com.ps.dao.person.EmployeeDAO;
import com.ps.entity.Result;
import com.ps.entity.Status;
import com.ps.global.ResultCode;

import com.ps.pojo.dept.Department;
import com.ps.pojo.dept.WorkPosition;
import com.ps.pojo.person.Employee;
import com.ps.pojo.setting.Area;
import com.ps.service.dept.DepartmentService;
import com.ps.service.dept.WorkPositionService;
import com.ps.service.person.EmployeeService;
import com.ps.service.setting.AreaService;
import com.ps.util.Kit;
import com.ps.util.R;

public class DepartmentAction extends BaseAction implements IFBaseAction {
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private WorkPositionService workPositionService;
	@Autowired
	private AreaService areaService;
    
	
    private Integer departmentId;

    private Integer orginDepartmentId;
    
    private Department department;
    
    private Integer fatherId;
    
    private Area area;
    
    private String departmentJson;
    
    private String workPositionJson;
    

    
    private String workPositionIds;
    
    private Integer puDepartmentId;
    
    private String prWorkPositionIds;
    
    private Result result;
    

    
	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public String add() {
		if (department.getFatherId() == -1) department.setFatherId(0);
		
		System.out.println(Kit.toJson(department));
		department.setInputerId(this.getSessionUserId());
		Integer [] wpIds = Kit.getFromJson(workPositionIds, Integer [].class);
		System.out.println(StringUtils.join(wpIds, ';'));
		this.result = departmentService.addDepartment(department, wpIds);
        System.out.println(Kit.toJson(result));
		return "main";
	}

	public String del() {
		if (departmentId == -1) {
			return "main";
		}
		if (departmentService.existsChild(departmentId)) {
			Result result = Kit.createResult();
			result.getStatus().setCode(ResultCode.EXISTS_CHILD_DEPT);
			result.getStatus().setMessage(R.string.getProperty("exist_child_dept"));
			result.setDatas(departmentId);
			setResult(result);
		} else if (departmentService.existsWorkPosition(departmentId)) {
			this.result = workPositionService.delWPAndCountPersonIsZero(departmentId, this.getSessionUserId());
			if (StringUtils.equals(result.getStatus().getCode(), ResultCode.DEL_SUCCES) )result.setDatas(fatherId); 
			
		} else {
			result = departmentService.delete(departmentId);
			
			if (StringUtils.equals(result.getStatus().getCode(), Status.SUCCES)) {
				result.getStatus().setCode(ResultCode.DEL_SUCCES);
				result.setDatas(fatherId); 
			} else {
				result.getStatus().setCode(ResultCode.DEL_FAIL);
			}
		}
		return "main";
	}

	@SuppressWarnings("unchecked")
	public String getById() {
		Result result = departmentService.queryDepartmentDetail(departmentId);
		this.outputJson(result);
		return null;
	}

	public String query() {
		
		return "main";
	}

	public String update() {
		 
        //System.out.println("workPositionIds = " + workPositionIds);
		Integer [] wpIds =  Kit.getFromJson(workPositionIds, Integer [].class);
		this.result = departmentService.updateDeptWPAndCountPersonIsZero(department, wpIds);
        System.out.println(Kit.toJson(this.result));
		return "main";
	}

	public String getXmlTree() {
		Integer areaId = this.getUserAreaId();
		String content = departmentService.findXmlTree(areaId);

		outputXml(content);
		
		return null;
	}
	
	public String moveToDept() {
		System.out.println("departmentId = " + departmentId + " orginDepartmentId = " + orginDepartmentId);
		this.result = workPositionService.moveToDept(departmentId, orginDepartmentId, this.getSessionUserId());
		return "main";
	}
		
	public String moveToDeptForUpt() {
		System.out.println("departmentId = " + departmentId + " orginDepartmentId = " + orginDepartmentId);
		System.out.println("this.puDepartmentId = " + this.puDepartmentId);
		System.out.println("this.prWorkPositionIds = " + this.prWorkPositionIds);
		Integer [] wpIds = Kit.getFromJson(prWorkPositionIds, Integer [].class);
		System.out.println("this.prWorkPositionIds = " + StringUtils.join(wpIds, ","));
		this.result = workPositionService.moveToDeptForUpt(departmentId, orginDepartmentId, wpIds,this.getSessionUserId());
		return "main";
	}
	
	public String jumpTo() {
		if (StringUtils.equals(jump, "add")) {
			if (fatherId != -1) {
				Department dept = departmentService.getBaseDao().findById(fatherId);
				this.setArea(dept.getArea());
			} 
			System.out.println("fatherId = " + this.fatherId);
		} else if (StringUtils.equals(jump, "wpToDept")) {
			if (this.departmentId == null) {
				return null;
			} else {
				this.result = workPositionService.findByProperty("department.departmentId", this.departmentId);
			}
		} else if (StringUtils.equals(jump, "wpToDeptForUpt")) {
			System.out.println("this.puDepartmentId = " + this.puDepartmentId);
			System.out.println("this.prWorkPositionIds = " + this.prWorkPositionIds);
			Integer [] wpIds = Kit.getFromJson(prWorkPositionIds, Integer [].class);
			System.out.println("this.prWorkPositionIds = " + StringUtils.join(wpIds, ","));
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(WorkPosition.class);
			detachedCriteria.add(Restrictions.eq("department.departmentId", puDepartmentId));
			detachedCriteria.add(Restrictions.in("wpId", wpIds));
			this.result = workPositionService.findByCriteria(detachedCriteria);
			
		}
		
		
		return jump;
	}	
	
	
	
	
	
	
	public void setDepartmentJson(String departmentJson) {
		this.departmentJson = departmentJson;
	}

	public String getDepartmentJson() {
		return departmentJson;
	}

	public void setWorkPositionJson(String workPositionJson) {
		this.workPositionJson = workPositionJson;
	}

	public String getWorkPositionJson() {
		return workPositionJson;
	}
    
    
	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Department getDepartment() {
		return department;
	}

	public String findByPage() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setWorkPositionIds(String workPositionIds) {
		this.workPositionIds = workPositionIds;
	}

	public String getWorkPositionIds() {
		return workPositionIds;
	}



	public void setFatherId(Integer fatherId) {
		this.fatherId = fatherId;
	}

	public Integer getFatherId() {
		return fatherId;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public Area getArea() {
		return area;
	}

	public void setOrginDepartmentId(Integer orginDepartmentId) {
		this.orginDepartmentId = orginDepartmentId;
	}

	public Integer getOrginDepartmentId() {
		return orginDepartmentId;
	}

	public void setPuDepartmentId(Integer puDepartmentId) {
		this.puDepartmentId = puDepartmentId;
	}

	public Integer getPuDepartmentId() {
		return puDepartmentId;
	}

	public void setPrWorkPositionIds(String prWorkPositionIds) {
		this.prWorkPositionIds = prWorkPositionIds;
	}

	public String getPrWorkPositionIds() {
		return prWorkPositionIds;
	}



	
}
